## Figure A5: Clustering of Closing Dates by Hour and Neighborhood Income

## install.packages(c("tidyverse"))
## library(tidyverse)

## SET WORKING DIRECTORY HERE
## setwd()

## Loading data
## load("dta.RData")

clustering_hour_inc = dta %>%
  filter(close_hour != "NA:00") %>%
  filter(!is.na(inc_third)) %>%
  mutate(n = 1,
         close_hour = factor(close_hour, levels = c("0:00", "1:00", "2:00", "3:00",
                                                    "4:00", "5:00", "6:00", "7:00",
                                                    "8:00", "9:00", "10:00", "11:00",
                                                    "12:00", "13:00", "14:00", "15:00",
                                                    "16:00", "17:00", "18:00",
                                                    "19:00", "20:00", "21:00",
                                                    "22:00", "23:00", "24:00"))) %>%
  group_by(inc_third, close_hour) %>%
  summarise(n_requests_closed = sum(n, na.rm = TRUE)) %>%
  ggplot(aes(x = factor(close_hour), y = n_requests_closed, fill = factor(inc_third))) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_x_discrete(labels = c("", "", "", "", "", "", "6:00", "",
                              "", "", "", "", "12:00", "", "", "",
                              "", "", "18:00", "", "", "", "", "",
                              "24:00")) +
  scale_fill_brewer(name = "Per Capita Income Tercile", palette = "Blues", labels = c("Top", "Middle", "Bottom")) +
  labs(x = "Hour",
       y = "# of Requests Closed") +
  theme_classic()+
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black"))